package com.xysl.controller;


import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.xysl.api.model.XyslLoginUser;
import com.xysl.api.domain.XyslUser;
import com.xysl.service.IXyslPermissionService;
import com.xysl.service.IXyslUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;

import java.util.Set;

/**
 * 用户Controller
 *
 * @author xysl
 * @date 2024-12-27
 */
@Tag(name = "xysl_App用户接口",description ="xysl_App用户接口")
@RestController
@RequestMapping("/user")
    public class XyslUserController extends BaseController
{
    @Autowired
    private IXyslUserService xyslUserService;

    @Autowired
    private IXyslPermissionService permissionService;
    /**
     * 获取当前用户信息
     *
     */
    @Operation(summary = "账号查询用户信息")
    @PostMapping("/info/{account}")
    public R<XyslLoginUser> info(@PathVariable("account")  String account)
    {
        XyslUser sysUser = xyslUserService.queryXyslUserByAccount(account);
        if (StringUtils.isNull(sysUser))
        {
            return R.fail("用户名或密码错误");
        }
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(sysUser);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(sysUser);
        XyslLoginUser vo = new XyslLoginUser();
        vo.setRoles(roles);
        vo.setPermissions(permissions);
        vo.setXyslUser(sysUser);
        return R.ok(vo);
    }

    @Operation(summary = "获取当前登录用户信息")
    @PostMapping("/userInfo")
    public R<XyslUser> userInfo(){
        XyslLoginUser xyslLoginUser = SecurityUtils.getXyslLoginUser();
        XyslUser user = xyslLoginUser.getXyslUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
//        ajax.put("roles", roles);
//        ajax.put("permissions", permissions);
        return R.ok(user);
    }



    /**
     * 获取用户详细信息
     */
    @Operation(summary = "根据id获取用户详细信息")
    @RequiresPermissions("enroll:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfoById(@PathVariable("id") Long id)
    {
        return success(xyslUserService.selectXyslUserById(id));
    }

}
